﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using IndicoInterface;
using System.Net;
using System.IO;
using Utilities;
using IndicoInterface.SimpleAgendaDataModel;

namespace t_IndicoInterface
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class TestAgendaInfo
    {
        public TestAgendaInfo()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestCtor()
        {
            string url = "http://indico.cern.ch/conferenceDisplay.py?confId=14475";
            AgendaInfo a = new AgendaInfo(url);
            Assert.IsTrue("14475" == a.ConferenceID);
            AgendaInfo a1 = new AgendaInfo("http://indico.cern.ch/conferenceOtherViews.py?confId=14475");
            Assert.IsTrue("14475" == a.ConferenceID, "Other random URL does not return proper conference ID");
            AgendaInfo a2 = new AgendaInfo(14475);
            Assert.IsTrue("14475" == a.ConferenceID, "Integer ctor does not return the proper conference ID");
            Assert.IsTrue(a.AgendaSite == "indico.cern.ch", "Agenda website is not right!");
            try
            {
                AgendaInfo a3 = new AgendaInfo("hi there");
                Assert.IsTrue(false, "Exception should have been thrown when invalid URL was passed");
            }
            catch (AgendaException)
            {
            }
            
        }

        /// <summary>
        /// Make sure we can deal with non-number types of agenda id's. Ick!!!
        /// </summary>
        [TestMethod]
        public void TestNonIntAgendaId()
        {
            string url = "http://indico.cern.ch/conferenceDisplay.py?confId=a042880";
            AgendaInfo info = new AgendaInfo(url);
            Assert.IsTrue(info.ConferenceID == "a042880");

        }

        [TestMethod]
        public void TestXMLGet()
        {
            string url = "http://indico.cern.ch/conferenceDisplay.py?confId=a042880";
            AgendaInfo a = new AgendaInfo(url);
            WebRequest req = WebRequest.Create(a.AgendaFullXML);
            using (WebResponse res = req.GetResponse())
            {
                TextReader r = new StreamReader(res.GetResponseStream());
                string data = r.ReadToEnd();
                Assert.IsTrue(data.StartsWith("<?xml"), "The return URL is not raw XML");
                res.Close();
            }
        }

        [TestMethod]
        public void TestGetPS()
        {
            /// For a particluar agenda there seems to be a missing ps from the talk list. At least,
            /// I'm going to call that a bug!

            string url = "http://indico.cern.ch/conferenceDisplay.py?confId=a042880";
            AgendaInfo info = new AgendaInfo(url);
            var meeting = info.GetNormalizedConferenceData();

            Assert.IsNotNull(meeting.Sessions, "Should be some sessions!");
            var s0 = meeting.Sessions[0];
            Assert.IsNotNull(s0, "Should be one session");
            Assert.IsNotNull(s0.Talks, "Shoudl be talks");
            Assert.IsTrue(s0.Talks.Length > 1, "Should be at least 2 talks!");
            var t1 = s0.Talks[1];
            Assert.IsTrue(t1.ID == "s1t14", "The second talk doesn't seem to be the correct talk!");
            Assert.IsTrue(t1.Title == "Brief EvtGen Tutorial", "Title is not right either");
            Assert.IsNotNull(t1.SlideURL, "Slide URL should not be null!");
        }

#if false
        [TestMethod]
        public void GetSimpleMeetingAgenda()
        {
            string url = "http://indico.cern.ch/conferenceDisplay.py?confId=33078";
            AgendaInfo a = new AgendaInfo(url);
            var data = a.GetFullConferenceData();
            Assert.IsTrue(data.ID == "33078", "Conference ID was not filled correctly");
            Assert.IsTrue(data.category == "B-tagging", "Catagory was incorrect!");
            Assert.IsTrue(data.title == "CSC Note 10: Dijet Calibration Working Group Meeting", "Title is not correct");
            Assert.IsTrue(data.startDate == "2008-04-29T16:30:00", "Start date is not right");
            Assert.IsTrue(data.endDate == "2008-04-29T19:25:00", "End date for meeting is not right");

            Assert.IsTrue(data.session == null, "The conference should have no sessions.");
            Assert.IsTrue(data.contribution.Length == 4, "Didn't find the expected 4 talks");
            Assert.IsTrue(data.contribution[0].ID == "2", "ID of first contribution talk is incorrect");
            Assert.IsTrue(data.contribution[0].startDate == "2008-04-29T16:30:00", "Start date of first contribution is not right");
            Assert.IsTrue(data.contribution[0].endDate == "2008-04-29T16:45:00", "End date of contribution is not right");
            Assert.IsTrue(data.contribution[0].speakers != null, "No speakers for talk!");
            Assert.IsTrue(data.contribution[0].speakers.Length == 1, "Speakers shoudl have only one entry in here!");
            Assert.IsTrue(data.contribution[0].speakers[0].users != null, "User should not be null!");
            Assert.IsTrue(data.contribution[0].speakers[0].users.Length == 1, "There should be only one user!");
            Assert.IsTrue(data.contribution[0].speakers[0].users[0].name != null, "User's name should be non-szero!");
            Assert.IsTrue(data.contribution[0].speakers[0].users[0].name.first == "", "User's name is wrong!");
            Assert.IsTrue(data.contribution[0].speakers[0].users[0].name.middle == "", "User's name is wrong!");
            Assert.IsTrue(data.contribution[0].speakers[0].users[0].name.last == "Orin Harris (University of Washington)", "User's name is wrong!");

            var firsttalk = data.contribution[0].material;
            Assert.IsTrue(firsttalk != null, "The material for first talk is a null!");
            Assert.IsTrue(firsttalk.Length == 1, "There should be only one type of material in the talk");
            Assert.IsTrue(firsttalk[0].ID == "slides", "The material type should be slides!");
            Assert.IsTrue(firsttalk[0].title == "Slides", "The material title should be slides!");
            Assert.IsTrue(firsttalk[0].files.file.Length == 1, "There should be only one file associated with this talk");
            Assert.IsTrue(firsttalk[0].files.file[0].name == "s8_29_04_08.pdf", "The talk file is named incorrectly");
            Assert.IsTrue(firsttalk[0].files.file[0].type == "pdf", "The talk file type is not pdf");
            Assert.IsTrue(firsttalk[0].files.file[0].url.StartsWith("http://indico.cern.ch"), "Url of talk doesn't look right");

            Assert.IsTrue(data.contribution[1].material[0].link == null, "Link for second talk doesn't seem to be correct.");
        }
#endif

#if false
        [TestMethod]
        public void GetConferenceMeetingAgenda()
        {
            AgendaInfo a = new AgendaInfo(14475);
            var data = a.GetFullConferenceData();

            Assert.IsTrue(data.ID == "14475", "Conference ID is incorrect.");
            Assert.IsTrue(data.category == "B-tagging", "Conference catagory is incorrect.");
            Assert.IsTrue(data.title == "ATLAS b-tagging workshop", "Conference title is incorrect");
            Assert.IsTrue(data.contribution == null, "The conference should have no contributions!");
            Assert.IsTrue(data.session != null, "The conference should have sessions.");
            Assert.IsTrue(data.session.Length == 11, "The meeting does not have the right number of sessions!");

            Assert.IsTrue(data.session[0].ID == "8", "The first session ID is not 8");
            Assert.IsTrue(data.session[1].ID == "11", "The second session ID is not 11");

            Assert.IsTrue(data.session[0].title == "Introduction I", "Session title is not right");
            Assert.IsTrue(data.session[0].startDate == "2007-05-10T09:15:00", "session start date is not right");
            Assert.IsTrue(data.session[0].endDate == "2007-05-10T10:30:00", "session start date is not right");

            Assert.IsTrue(data.session[0].contribution.Length == 5, "First session has incorrect number of contributions");
            Assert.IsTrue(data.session[1].contribution.Length == 4, "Second session has incorrect number of contributions");

            Assert.IsTrue(data.session[0].contribution[0].ID == "21", "First contribution ID is not correct");
            Assert.IsTrue(data.session[0].contribution[4].ID == "23", "First contribution ID is not correct");

            /// Won't test the internals of the contribution as we expect the XML to be the same as in the simple case.
        }
#endif

        [TestMethod]
        public void GetNormalSimpleMeeting()
        {
            string url = "http://indico.cern.ch/conferenceDisplay.py?confId=a042880";
            AgendaInfo a = new AgendaInfo(url);
            var data = a.GetNormalizedConferenceData();

            Assert.IsTrue(data.ID == "a042880", "Conference ID is incorrect.");
            Assert.IsTrue(data.Site == "indico.cern.ch", "Conference site is incorrect");
            Assert.IsTrue(data.Title == "EvtGen miniworkshop", "Title is not right");
            Assert.IsTrue(data.StartDate == new DateTime(2005, 01, 21, 9, 0, 0), "Start date is not right");
            Assert.IsTrue(data.EndDate == new DateTime(2005, 01, 21, 19, 0, 0), "End date is not right");

            Assert.IsTrue(data.Sessions.Length == 1, "Should have only a single session");
            Assert.IsTrue(data.Sessions[0].ID == "0", "Default session ID is not set correctly");

            var ses = data.Sessions[0];
            Assert.IsTrue(ses.Title == data.Title, "Session title should be the same as meeting title");
            Assert.IsTrue(ses.StartDate == data.StartDate, "Session start date should match meeting start date");
            Assert.IsTrue(ses.EndDate == data.EndDate, "Session end date should match meeting end date");

            Assert.IsTrue(ses.Talks.Length == 14, "Incorrect number of talks in session!");

            var talk1 = ses.Talks[0];
            Assert.IsTrue(talk1.ID == "s1t15", "ID of talk is not correct");
            Assert.IsTrue(talk1.Title == "Introduction to the EvtGen Mini Workshop", "Talk title is not right");
            Assert.IsTrue(talk1.StartDate == new DateTime(2005, 01, 21, 9, 0, 0), "Start time of talk is not correct");
            Assert.IsTrue(talk1.EndDate == new DateTime(2005, 01, 21, 9, 15, 0), "End time of talk is not right");
            Assert.IsNotNull(talk1.SlideURL, "The URL for the slides should not be null!");
            Assert.IsTrue(talk1.SlideURL.StartsWith("http://indico.cern.ch"), "Slide URL is not correct");
            Assert.IsTrue(talk1.Speakers != null, "Speaker list should not be null!");
            Assert.IsTrue(talk1.Speakers.Length == 1, "Should be only one speaker");
            Assert.IsTrue(talk1.Speakers[0] == "Bartalini, P.", "Speakers name is not correct");
        }

        [TestMethod]
        public void GetFullSimpleMeeting()
        {
            string url = "http://indico.cern.ch/conferenceDisplay.py?confId=a042880";
            AgendaInfo a = new AgendaInfo(url);
            var data = a.GetFullConferenceData();

            /// Just some simple tests to help pin things down.

            Assert.IsTrue(data != null, "Null data came back - crazy!");
            Assert.IsTrue(data.session == null, "Should be only one session");
            Assert.IsTrue(data.contribution.Length > 0, "Should be some contributions at the top level here");

            var talk1 = data.contribution[0];
            Assert.IsTrue(talk1.title == "Introduction to the EvtGen Mini Workshop", "Title is incorrect");
            Assert.IsTrue(talk1.material != null, "Material should not be null!");
            Assert.IsTrue(talk1.material.Length > 0, "We should have some material for this talk!");
            Assert.IsTrue(talk1.material[0].pdf != null, "The PDF link is null");
            Assert.IsTrue(talk1.material[0].pdf.StartsWith("http://indico.cern.ch"), "The pdf link does not start correctly");
        }
#if false
        /// Since CERN turned on authenticaion, this particular guy is no longer accessible, so we will skip this test. When I get around to implementing
        /// authentication, then this will be back in...
        [TestMethod]
        public void GetNormalSimpleMeetingAgenda()
        {
            /// Make sure we normalize this guy correctly.
            string url = "http://indico.cern.ch/conferenceDisplay.py?confId=33078";
            AgendaInfo a = new AgendaInfo(url);
            var data = a.GetNormalizedConferenceData();

            Assert.IsTrue(data.ID == 33078, "Conference ID is incorrect.");
            Assert.IsTrue(data.Site == "indico.cern.ch", "Site is incorrect");
            Assert.IsTrue(data.Title == "CSC Note 10: Dijet Calibration Working Group Meeting", "Conference title is incorrect.");
            Assert.IsTrue(data.StartDate == new DateTime(2008, 04, 29, 16, 30, 0), "Meeting start date is not right");
            Assert.IsTrue(data.EndDate == new DateTime(2008, 04, 29, 19, 25, 0), "Meeting end date is not right");

            Assert.IsTrue(data.Sessions.Length == 1, "Should be only a single session in this meeting");
            Assert.IsTrue(data.Sessions[0].ID == 0, "Null session for meeting should have ID 0");

            Assert.IsTrue(data.Sessions[0].Title == data.Title, "Session title is not the same as the meeting title!");
            Assert.IsTrue(data.Sessions[0].StartDate == data.StartDate, "Session start time is not the same as the meeting start time.");
            Assert.IsTrue(data.Sessions[0].EndDate == data.EndDate, "Session end date is not right either");

            Assert.IsTrue(data.Sessions[0].Talks.Length == 4, "Wrong number of talks in the conference");
            Assert.IsTrue(data.Sessions[0].Talks[0].SlideURL.StartsWith("http://indico.cern.ch"), "URL is not correct");
            Assert.IsTrue(data.Sessions[0].Talks[0].ID == 2, "ID of first talk is not right");
            Assert.IsTrue(data.Sessions[0].Talks[0].Title == "Performance of alternative implementations of System 8", "Title of first talk is not correct");
            Assert.IsTrue(data.Sessions[0].Talks[0].StartDate == new DateTime(2008, 4, 29, 16, 30, 0), "start time of talk is not right");
            Assert.IsTrue(data.Sessions[0].Talks[0].EndDate == new DateTime(2008, 4, 29, 16, 45, 0), "start time of talk is not right");
            Assert.IsTrue(data.Sessions[0].Talks[0].Speakers != null, "Speaker array should not be zero!");
            Assert.IsTrue(data.Sessions[0].Talks[0].Speakers.Length == 1, "Speaker array should be of length 1");
            Assert.IsTrue(data.Sessions[0].Talks[0].Speakers[0] == "Orin Harris (University of Washington)", "Speaker's name is not right!");

            Assert.IsTrue(data.Sessions[0].Talks[1].ID == 3, "ID of second talk not right");
            Assert.IsTrue(data.Sessions[0].Talks[1].SlideURL.StartsWith("http://indico.cern.ch"), "URL of second talk not right!");
        }
#endif

#if false
        [TestMethod]
        public void GetNormalConferenceMeetingAgenda()
        {
            AgendaInfo a = new AgendaInfo(14475);
            var data = a.GetNormalizedConferenceData();

            Assert.IsTrue(data.ID == "14475", "Conference ID is incorrect.");
            Assert.IsTrue(data.Title == "ATLAS b-tagging workshop", "Conference title is incorrect");
            Assert.IsTrue(data.StartDate == new DateTime(2007, 05, 10, 9, 00, 00), "Start date of meeting is not right!");
            Assert.IsTrue(data.EndDate == new DateTime(2007, 05, 11, 18, 00, 00), "Start date of meeting is not right!");

            Assert.IsTrue(data.Sessions != null, "The conference should have sessions.");
            Assert.IsTrue(data.Sessions.Length == 11, "The meeting does not have the right number of sessions!");

            Assert.IsTrue(data.Sessions[0].ID == "8", "The first session ID is not 8");
            Assert.IsTrue(data.Sessions[1].ID == "11", "The second session ID is not 11");

            Assert.IsTrue(data.Sessions[0].Title == "Introduction I", "Title of first session is not correct!");
            Assert.IsTrue(data.Sessions[0].StartDate == new DateTime(2007, 05, 10, 9, 15, 0), "Start time is not correct");
            Assert.IsTrue(data.Sessions[0].EndDate == new DateTime(2007, 05, 10, 10, 30, 0), "Start time is not correct");

            Assert.IsTrue(data.Sessions[0].Talks.Length == 5, "First session has incorrect number of contributions");
            Assert.IsTrue(data.Sessions[1].Talks.Length == 4, "Second session has incorrect number of contributions");

            Assert.IsTrue(data.Sessions[0].Talks[0].ID == "21", "First contribution ID is not correct");
            Assert.IsTrue(data.Sessions[0].Talks[4].ID == "23", "First contribution ID is not correct");
        }
#endif

        [TestMethod]
        public void GetWisconsonAgenda()
        {
            AgendaInfo a = new AgendaInfo("http://agenda.hep.wisc.edu/conferenceOtherViews.py?view=standard&confId=60");
            Assert.IsTrue(a.ConferenceID == "60", "Conference ID is incorrect!");
            Assert.IsTrue(a.AgendaSite == "agenda.hep.wisc.edu", "Agenda website is not right!");

            var data = a.GetNormalizedConferenceData();
            Assert.IsTrue(data.Title == "Pheno 2008 Symposium", "Agenda title is not right");
        }

        [TestMethod]
        public void TestPDFLink()
        {
            AgendaInfo a = new AgendaInfo("http://agenda.hep.wisc.edu/conferenceOtherViews.py?view=standard&confId=60");
            Assert.IsTrue(a.ConferenceID == "60", "Conference ID is incorrect!");
            Assert.IsTrue(a.AgendaSite == "agenda.hep.wisc.edu", "Agenda website is not right!");

            var data = a.GetNormalizedConferenceData();
            var session1 = data.Sessions[0];
            Assert.IsTrue(session1.Title == "Plenary 1", "The title of the first session is incorrect");
            var talk1 = session1.Talks[1];
            Assert.IsTrue(talk1.Title == "SM Tests at the Tevatron Collider", "Title of talk 2 is incorrect");
            Assert.IsTrue(talk1.SlideURL == "http://agenda.hep.wisc.edu/getFile.py/access?contribId=1&sessionId=10&resId=0&materialId=slides&confId=60", "Slide URL for PDFS for talk 1 is not right");
        }

        [TestMethod]
        public void TestPPTLink()
        {
            AgendaInfo a = new AgendaInfo("http://agenda.hep.wisc.edu/conferenceOtherViews.py?view=standard&confId=60");
            Assert.IsTrue(a.ConferenceID == "60", "Conference ID is incorrect!");
            Assert.IsTrue(a.AgendaSite == "agenda.hep.wisc.edu", "Agenda website is not right!");

            var data = a.GetNormalizedConferenceData();
            var session1 = data.Sessions[2];
            Assert.IsTrue(session1.Title == "Supersymmetry Breaking", "The title of the first session is incorrect");
            var talk1 = session1.Talks[2];
            Assert.IsTrue(talk1.Title == "Discovering Sparticles in the CMSSM and GUT-less SUSY-Breaking Scenarios", "Title of talk 2 is incorrect");
            Assert.IsTrue(talk1.SlideURL == "http://agenda.hep.wisc.edu/getFile.py/access?contribId=85&sessionId=15&resId=0&materialId=slides&confId=60", "Slide URL for PPT for talk 1 is not right");
        }

        [TestMethod]
        public void GetFermiIndicoTalk()
        {
            /// Something goes wrong when we try to get a talk from Fermilab
            AgendaInfo a = new AgendaInfo("http://indico.fnal.gov/conferenceTimeTable.py?confId=1829");

            Assert.IsTrue(a.ConferenceID == "1829", "Conference ID is incorrect");
            Assert.IsTrue(a.AgendaSite == "indico.fnal.gov", "Agenda web site is not right");

            var data = a.GetNormalizedConferenceData();
            Assert.IsTrue(data.Title == "Hadron Collider Physics Symposium 2008 - HCP08", "Conference title is not right");
        }

        [TestMethod]
        public void GetPPTOverPDF()
        {
            /// Something goes wrong when we try to get a talk from Fermilab
            AgendaInfo a = new AgendaInfo("http://indico.fnal.gov/conferenceTimeTable.py?confId=1829");

            var data = a.GetNormalizedConferenceData();

            Session s = data.Sessions[0];
            Talk t = s.Talks[2];
            Assert.IsTrue(t.Title == "Top Quark Mass and Cross Section Results from the Tevatron", "Title of talk 3 isn't right");
            Assert.IsTrue(t.SlideURL == "http://indico.fnal.gov/getFile.py/access?contribId=2&sessionId=0&resId=0&materialId=slides&confId=1829", "URL for PDF is not right");

            s = data.Sessions[1];
            t = s.Talks[0];
            Assert.IsTrue(t.Title == "W/Z Properties at the Tevatron", "title of session 2 talk not right");
            Assert.IsTrue(t.SlideURL == "http://indico.fnal.gov/getFile.py/access?contribId=6&sessionId=1&resId=2&materialId=slides&confId=1829", "Slide URL of ppt is not right");
        }

        [TestMethod]
        public void LookForBadTalkMessages()
        {
            /// See if we can find any error messages for talks that don't have anything in them.
            /// Something goes wrong when we try to get a talk from Fermilab
            /// 

            FileInfo log = new FileInfo("errors.txt");
            if (log.Exists)
            {
                log.Delete();
            }
            Logger.UseFileLogger(log);

            try
            {
                //delete
                //AgendaInfo a = new AgendaInfo("http://indico.cern.ch/conferenceDisplay.py?confId=27771");
                AgendaInfo a = new AgendaInfo("http://indico.cern.ch/conferenceDisplay.py?confId=a042880");
                var data = a.GetNormalizedConferenceData();
            }
            finally
            {
                Logger.ResetTestLogging();
            }

            log.Refresh();
            Assert.IsTrue(log.Length > 100, "Error file is empty!");
            TextReader rdr = log.OpenText();
            int count = 0;
            string line;
            while ((line = rdr.ReadLine()) != null)
            {
                if (line.Contains("No usable talk slides"))
                {
                    count = count + 1;
                }
            }
            Assert.IsTrue(count > 1, "Unable to find any error messages for missing PDFs or the like!");
        }

        /// <summary>
        /// Make sure we can make one of these XML and back again.
        /// </summary>
        [TestMethod]
        public void TestAgendaSerialization()
        {
            StringWriter sw = new StringWriter();

            AgendaInfo ai = new AgendaInfo("http://indico.fnal.gov/conferenceTimeTable.py?confId=1829");

            ai.Seralize(sw);

            string xml = sw.ToString();

            Assert.IsTrue(xml != null, "the xml translation shouldn't be null!");

            StringReader rdr = new StringReader(xml);
            AgendaInfo aiback = AgendaInfo.Deseralize(rdr);

            Assert.IsTrue(aiback != null, "Null agenda came back!");
            Assert.IsTrue(aiback.ConferenceID == ai.ConferenceID, "Conference ID is not correct");
            Assert.IsTrue(ai.AgendaSite == aiback.AgendaSite, "Conference site is not correct!");

            AgendaInfo aistringback = AgendaInfo.Deseralize(xml);
            Assert.IsTrue(aistringback != null, "Null agenda came back!");
            Assert.IsTrue(aistringback.ConferenceID == ai.ConferenceID, "Conference ID is not correct");
            Assert.IsTrue(ai.AgendaSite == aistringback.AgendaSite, "Conference site is not correct!");
        }

        [TestMethod]
        public void TestAgendaDeseralizationWithNoSub()
        {
            /// We've added the sub directory. Make sure that the XML can be delt with!

            AgendaInfo ai = new AgendaInfo("http://indico.fnal.gov/conferenceTimeTable.py?confId=1829");

            string xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?><AgendaInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><ConferenceID>1829</ConferenceID><AgendaSite>indico.fnal.gov</AgendaSite></AgendaInfo>";

            StringReader rdr = new StringReader(xml);
            AgendaInfo back = AgendaInfo.Deseralize(rdr);

            Assert.AreEqual(ai.AgendaFullXML, back.AgendaFullXML, "The http requests are not the same! Ops!");
        }

        [TestMethod]
        public void TestAgendaDeseralizationWithSub()
        {
            /// We've added the sub directory. Make sure that the XML can be delt with!

            AgendaInfo ai = new AgendaInfo("http://indico.fnal.gov/bogus/conferenceTimeTable.py?confId=1829");

            string xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?><AgendaInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><ConferenceID>1829</ConferenceID><AgendaSite>indico.fnal.gov</AgendaSite><AgendaSubDirectory>bogus</AgendaSubDirectory></AgendaInfo>";

            StringReader rdr = new StringReader(xml);
            AgendaInfo back = AgendaInfo.Deseralize(rdr);

            Assert.AreEqual(ai.AgendaFullXML, back.AgendaFullXML, "The http requests are not the same! Ops!");
        }

        [TestMethod]
        public void TestHTTPS()
        {
            AgendaInfo ai_s = new AgendaInfo("https://indico.desy.de/conferenceOtherViews.py?view=standard&confId=1356");
            AgendaInfo ai = new AgendaInfo("http://indico.desy.de/conferenceOtherViews.py?view=standard&confId=1356");

            Assert.AreEqual("1356", ai_s.ConferenceID, "Incorrect agenda ID");

            ///
            /// This next test -- various things deeper in the system depend on the serialized string being the same
            /// or at lesat, its hash code. Major changes would have to be made if that weren't the case.
            /// 

            StringWriter wr_s = new StringWriter();
            StringWriter wr = new StringWriter();

            ai_s.Seralize(wr_s);
            ai.Seralize(wr);

            string swr_s = wr_s.ToString();
            string swr = wr.ToString();

            Assert.AreEqual(swr.GetHashCode(), swr_s.GetHashCode(), "The hash codes of the two serialized strings are not the same.");            
        }

        [TestMethod]
        public void TestSubDayLinks()
        {
            AgendaInfo ai = new AgendaInfo("http://indico.desy.de/conferenceOtherViews.py?view=standard&confId=1356#2009-01-07");
            Assert.AreEqual("1356", ai.ConferenceID, "The conference ID is not correct!");
        }

        [TestMethod]
        public void TestNoSubDirectory()
        {
            AgendaInfo ai = new AgendaInfo("http://indico.desy.de/conferenceOtherViews.py?view=standard&confId=1356#2009-01-07");
            Assert.AreEqual("", ai.AgendaSubDirectory, "Sub directory isn't blank");
        }

        [TestMethod]
        public void TestIndicoInSubDir()
        {
            AgendaInfo ai = new AgendaInfo("http://indico.ific.uv.es/indico/conferenceDisplay.py?confId=62");
            Assert.AreEqual("62", ai.ConferenceID, "The conference ID is not correct!");
            Assert.AreEqual("indico", ai.AgendaSubDirectory, "The subdirectory isn't right");
            Assert.AreEqual("indico.ific.uv.es", ai.AgendaSite, "The agenda site isn't right");
            Assert.IsTrue(ai.AgendaFullXML.StartsWith("http://indico.ific.uv.es/indico"), "The full XML agenda guy doesn't start correctly!");
        }
    }
}
